/*
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.

SPDX-License-Identifier: Apache-2.0
*/

package main

import (
	"fmt"
	"log"
	"strconv"

	"chainmaker.org/chainmaker/pb-go/v2/common"
	sdk "chainmaker.org/chainmaker/sdk-go/v2"
	"chainmaker.org/chainmaker/sdk-go/v2/examples"
)

const (
	createContractTimeout    = 5
	claimVersion             = "1.0.0"
	claimContractName        = "TRANSACTION_MANAGER"
	claimByteCodePath        = ""
	sdkConfigPKUser1Path     = "../sdk_configs/sdk_config_pk_user1.yml"
	sdkConfigOrg1Client1Path = "../sdk_configs/sdk_config_org1_client1.yml"
)

func main() {
	testUserContractBlacklistAdd()
}

func testUserContractBlacklistAdd() {
	fmt.Println("====================== create client ======================")
	//client, err := examples.CreateChainClientWithSDKConf(sdkConfigPKUser1Path)
	client, err := examples.CreateChainClientWithSDKConf(sdkConfigOrg1Client1Path)
	if err != nil {
		log.Fatalln(err)
	}
	// s, err := client.GetBlockByHeight(1223, true)
	// fmt.Println(s)
	// fmt.Println("====================== 调用合约 ======================")
	// txIds, err := testUserContractClaimInvoke(client, "ADD_BLACKLIST_TX_IDS", true)
	// if err != nil {
	// 	log.Fatalln(err)
	// }
	// fmt.Println("====================== 调用完成 ======================")
	var n [1000]string
	fmt.Println("====================== 调用合约100次 ======================")
	for i := 0; i < 1; i++ {
		txIds, err := testUserContractClaimInvoke(client, "ADD_BLACKLIST_TX_IDS", true)
		n[i] = txIds
		if err != nil {
			log.Fatalln(err)
		}
	}
	fmt.Println("====================== 调用完成 ======================")
	fmt.Println(n)

}

func testUserContractClaimInvoke(client *sdk.ChainClient,
	method string, withSyncResult bool) (string, error) {
	// curTime := strconv.FormatInt(time.Now().Unix(), 10)
	txIds := ""
	txId := "abc"
	// txId := uuid.GetUUID()
	// fmt.Println(txId)
	for i := 0; i < 100; i++ {
		// fmt.Println(txId + strconv.Itoa(i))
		txIds = txIds + txId + strconv.Itoa(i) + ","
	}
	n := len(txIds)
	txIds = txIds[:n-1]
	// fmt.Println(txIds)
	kvs := []*common.KeyValuePair{
		{
			Key:   "txIds",
			Value: []byte(txIds),
		},
	}
	// fmt.Println(claimContractName)
	// fmt.Println(method)
	fmt.Println(kvs)

	err := invokeUserContract(client, claimContractName, method, "", kvs, withSyncResult, &common.Limit{GasLimit: 200000})
	if err != nil {
		return "", err
	}

	// return "q23", ni
	return txIds, nil
}

func invokeUserContract(client *sdk.ChainClient, contractName, method, txId string,
	kvs []*common.KeyValuePair, withSyncResult bool, limit *common.Limit) error {
	fmt.Println(kvs)

	resp, err := client.InvokeContractWithLimit(contractName, method, txId, kvs, -1, withSyncResult, limit)
	if err != nil {
		return err
	}
	fmt.Println(resp)
	if resp.Code != common.TxStatusCode_SUCCESS {
		return fmt.Errorf("invoke contract failed, [code:%d]/[msg:%s]\n", resp.Code, resp.Message)
	}

	if !withSyncResult {
		fmt.Printf("invoke contract success, resp: [code:%d]/[msg:%s]/[txId:%s]\n", resp.Code, resp.Message, resp.ContractResult.Result)
	} else {
		fmt.Printf("invoke contract success, resp: [code:%d]/[msg:%s]/[contractResult:%s]\n", resp.Code, resp.Message, resp.ContractResult)
	}

	return nil
}
